<template>
  <div class="goods_cart">
    <!-- 顶部导航栏 -->
    <div class="top">
      <van-nav-bar left-arrow class="top-wrap">
        <template #left>
          <div class="yuanquan" @click="tohome">
            <van-icon name="wap-home-o" class="home-icon" />
          </div>
        </template>
        <template #right>
          <van-search
            left-icon=""
            class="search"
            shape="round"
            background="white"
            placeholder="搜索商品"
            input-align="center"
            @click="tosearch"
          >
            <template #left-icon>
              <van-icon name="search" class="search-icon" />
            </template>
          </van-search>
        </template>
      </van-nav-bar>
    </div>

    <!-- 侧边导航栏  -->
    <div class="aside">
      <van-sidebar v-model="activeKey" class="aside-wrap">
        <van-sidebar-item
          v-for="(item, index) in list"
          :key="index"
          :title="item.name"
          @click="check(), getProducts(item.id), getTopList(item)"
        />
      </van-sidebar>
    </div>

    <!-- 内容导航栏 -->
    <div class="content">
      <van-tabs
        type="card"
        class="cont-wrap"
        @click="onClick"
        :ellipsis="false"
      >
        <div>
          <van-tab
            :name="card.id"
            v-for="card in toplist"
            :key="card.id"
            :title="card.name"
          >
            <div class="goodspic" v-for="item in goods" :key="item.id">
              <div class="pic">
                <div class="imgbox">
                  <img class="cont-img" :src="item.image" alt="" />
                </div>
                <p>{{ item.storeName }}</p>
                <span class="allqian">
                  <span class="qian">¥</span>
                  {{ item.price }}
                  <span class="sales">已售{{ item.sales }}</span>
                  <button class="toshow">
                    <van-cell class="tocart" @click="showPopup(item.id)" 
                      >加入购物车</van-cell
                    >
                  </button>
                </span>
              </div>
            </div>
            <van-loading class="more" v-if="loading"> 加载中.... </van-loading>
            <div
              v-else
              :disabled="hasmore"
              class="more"
              @click="getmore(card.id)"
            >
              {{ hasmore ? "已经到底啦" : "加载更多" }}
            </div>
          </van-tab>

          <!-- <div class="dropmenu" @click="seemore"></div> -->
        </div>
      </van-tabs>
      <van-popup
        v-model="show"
        round
        closeable
        position="bottom"
       
      >
        <SomeOfGood :goodid="goodid"></SomeOfGood>
        <!-- <button>加入购物车</button> -->
      </van-popup>
    </div>
    <!-- 底部结算 -->
    <div class="bottom">
      <van-submit-bar :price="3050" button-text="提交订单" @submit="onSubmit">
        <div class="cart-icon">
          <van-icon name="cart-o" size="25px" class="cart-img" color="white" />
        </div>
      </van-submit-bar>
    </div>
  </div>
</template>
<script>
import axios from "axios";
import SomeOfGood from '../../components/someofgood';
export default {
  data() {
    return {
      goodid:0,
      show: false,
      activeKey: 0,
      list: [],
      toplist: [],
      goods: [],
      gooddetail: [],
      pageNo: 0,
      hasmore: false,
      loading: false,
    };
  },
  components:{
    SomeOfGood
  },

  created() {
    // console.log(this.list);
    // console.log(hah);
    // this.getTopList(hah[0].child)
  },
  mounted() {
    this.showAll();
  },
  methods: {
    tohome() {
      this.$router.push("/home");
    },
    tosearch() {
      this.$router.push("/goods_search");
    },
    godetail(url, id) {
      this.$router.push(url + "/" + id);
    },
    onSubmit() {},
    seemore() {},
    showPopup(id) {
      this.show = true;
      // console.log(id);
      this.goodid=id
    },
 
    addcart(id) {
      const goodinfo = {
        id: id,
        image: image,
        price: price,
      };
      this.$store.dispatch("cart/addcart", goodinfo);
    },
    getmore(cid) {
      this.loading = true;
      setTimeout(() => {
        this.loading = false;
        this.getProducts(cid);
      }, 1000);
    },

    check() {
      this.hasmore = false;
      this.goods = [];
      this.pageNo = 0;
    },

    getTopList(item) {
      this.toplist = item.child;
    },
    onClick(name) {
      this.hasmore = false;
      this.goods = [];
      this.pageNo = 0;
      this.getProducts(name);
    },

    async getProducts(cid) {
      console.log(this.goods);
      console.log(this.hasmore);
      if (this.hasmore !== false) {
        return;
      }
      ++this.pageNo;
      // const res = await this.$http.getProducts({
      //   page: this.pageNo,
      //   limit: 10,
      //   type: 1,
      //   cid: cid,
      // });
      // `/api/front/products?page=${page}&limit=${limit}&type=${type}&cid=${cid}`
      await axios
        .get(`https://api.java.crmeb.net/api/front/products/`, {
          params: {
            page: this.pageNo,
            limit: 10,
            type: 1,
            cid: cid,
          },
        })
        .then((res) => {
          this.goods = this.goods.concat(res.data.data.list);
          this.hasmore = res.data.data.total < this.pageNo * 10;
        });
      // this.goods = this.goods.concat(res.data.data.list);
      // this.hasmore = res.data.data.total < this.pageNo * 10;
    },
    async showAll() {
      await axios
        .get(`https://api.java.crmeb.net/api/front/category`)
        .then((res) => {
          res.data.data.forEach((item) => {
            item.child.unshift({
              id: item.id,
              name: "全部",
            });
          });
          this.list = res.data.data;
          this.getTopList(res.data.data[0]);
          this.getProducts(515);
        });
    },

    async getList() {
      // const res = await this.$http.getList();
      await axios
        .get(`https://api.java.crmeb.net/api/front/category`)
        .then((res) => {
          res.data.data.forEach((item) => {
            item.child.unshift({
              id: item.id,
              name: "全部",
            });
          });
          this.list = res.data.data;
        });
    },
  },
};
</script>
<style lang="scss" >
.goods_cart {
  .more {
    color: #969799;
    font-size: 12px;
    line-height: 40px;
    text-align: center;
    margin-bottom: 50px;
  }
  .goodspic {
    // margin-top: 114px;
    .pic {
      // margin-top:114px ;
      width: 100%;
      box-sizing: border-box;
      margin-bottom: 31px;
      padding: 0 12px;
      .toshow {
        background-color: red;
        padding: 0 15px;
        height: 24px;
        line-height: 24px;
        border-radius: 21px;
        font-size: 13px;
        color: #fff;
        // position: relative;
        border: none;
        float: right;
        .van-cell__value--alone {
          color: white;
          // width: 50px;
          padding: 0;
        }
        .tocart {
          background-color: red;
          height: 24px;
          line-height: 24px;
          font-size: 13px;
          color: #fff;
          padding: 0 15px;
          //  width: 70px;
        }
      }
      .sales {
        font-size: 12px;
        font-family: PingFang SC;
        font-weight: 400;
        padding-left: 8px px;
        color: #8e8e8e;
      }
      .allqian {
        font-size: 21px;
        font-weight: 700;
        margin-right: 9px;
        color: red;
        .qian {
          font-size: 14px;
          color: red;
          // font-weight: 700;
          // font-weight: bold;
        }
      }

      p {
        font-size: 14px;
        font-weight: bold;
        overflow: hidden;
        display: -webkit-box;
        -webkit-box-orient: vertical;
        -webkit-line-clamp: 2;
      }
      .imgbox {
        height: 107px;
        border-radius: 7px;
        // padding:0 6px ;
        // margin: 0 12px;
        box-sizing: content-box;
        overflow: hidden;
        position: relative;
        .cont-img {
          display: block;
          width: 100%;
          position: absolute;
          top: -50px;
          // right: 6px;
          border-radius: 10px;
        }
      }
    }
  }

  .cart-icon {
    width: 48px;
    height: 48px;
    background-color: #cbcbcb;
    border-radius: 50%;
    position: relative;
    margin-top: -18px;
    .cart-img {
      position: absolute;
      left: 11px;
      top: 13px;
    }
  }

  .van-nav-bar__content {
    // position: fixed;
    height: 62px;
    .van-nav-bar__right {
      position: static;
    }
  }
  .van-field__body {
    width: 70px;
  }
  .van-nav-bar {
    .search-icon {
      color: #c8c9cc;
      margin-left: 90px;
      // position: fixed;
    }
  }
  .search {
    width: 310px;
  }
  .yuanquan {
    box-sizing: border-box;
    width: 33px;
    height: 33px;
    border-radius: 50%;
    background-color: red;
    text-align: center;
    line-height: 33px;
    // position: fixed;
  }
  .home-icon {
    color: white;
  }
  .van-sidebar-item {
    padding: 16px 12px;
    font-size: 13px;
  }
  .top-wrap {
    // float: left;
    // position: fixed;
  }
  .top {
    background-color: chartreuse;
    height: 62px;
    width: 100%;
    position: fixed;
    z-index: 99;
  }
  .aside {
    background-color: #f7f8fa;
    height: 571px;
    width: 22%;
    position: fixed;
    // height: 600px;
    margin-top: 62px;
  }
  .aside-wrap {
    position: fixed;
    top: 62px;
    z-index: 99;
  }
  .van-sidebar {
    width: 22%;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }
  .content {
    // padding-bottom:100px ;
    // padding-top: 50px;
    .dropmenu {
      background-color: darkblue;
      width: 20px;
      height: 20px;
      position: fixed;
      top: 75px;
      right: 15px;
      z-index: 100;
    }
    .van-tabs__content {
      margin-top: 114px;
    }
    .cont-wrap {
      float: right;
      width: 78%;

      .van-card {
        margin-top: 114px;
      }
      .van-tabs__wrap {
        width: 303px;
        padding-top: 11px;
        z-index: 99;
        position: fixed;
        top: 62px;
        left: 100px;
        background-color: white;

        .van-tab__text--ellipsis {
          width: 60px;
          text-align: center;
        }
        .van-tab__text {
          width: 60px;
          text-align: center;
        }

        .van-tabs__nav--card {
          border: none;
          box-sizing: border-box;
          height: 30px;
          margin-right: 33px;
          padding-right: 50px;
        }
        .van-tab {
          color: black;
          border-radius: 22px;
          height: 22px;
          background-color: #f7f8fa;
          margin: 2px 4px;
          border: none;
        }
        .van-tab--active {
          color: red;
          font-weight: bold;
          border-radius: 22px;
          height: 22px;
          background-color: #fdebe9;
          margin: 2px 4px;
        }
      }
    }
  }
}
</style>